# -*- coding: utf-8 -*-
from urllib import urlopen
import sys
import os
import logging
import datetime
import socket
socket.setdefaulttimeout(30.0)
from django.utils import simplejson
from django.conf import settings
from xiaonei import Xiaonei
from wuyao import Wuyao
from dramas.main.settings import category_image_group

def url_read(url, time = 5):
    try:
        return urlopen(url).read()
    except:
        if time > 0:
            return url_read(url, time - 1)
        else:
            raise

def get_cover(cate,id):
    if settings.DEBUG:
        return '%supload/%s/%s.jpg' % (settings.MEDIA_URL, category_image_group[cate], id)
    
def get_json(request):
    request_obj = {}
    for k,v in request.REQUEST.items(): 
        request_obj[k] = v
    return simplejson.dumps(request_obj)


def get_client(request):
    if 'xn_sig_user' in request.REQUEST:
        return Xiaonei(request)
    elif '51_sig_user' in request.REQUEST:
        return Wuyao(request)
    else:
        return None